/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpStorageDept;
import com.jims.master.modules.csm.service.ExpStorageDeptService;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.service.OrgDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 消耗品库存单位字典表Controller
 *
 * @author DT
 * @version 2017-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expStorageDept")
public class ExpStorageDeptController extends BaseController {

    @Autowired
    private ExpStorageDeptService expStorageDeptService;

    @Autowired
    private OrgDeptService orgDeptService;

    @ModelAttribute
    public ExpStorageDept get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return expStorageDeptService.get(id);
        } else {
            return new ExpStorageDept();
        }
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ExpStorageDept expStorageDept, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpStorageDept> page = expStorageDeptService.findPage(new Page<ExpStorageDept>(request, response), expStorageDept);
        model.addAttribute("page", page);
        return "modules/csm/expStorageDeptIndex";
    }

    /**
     * 添加跳转及获取科室分组名
     *
     * @param expStorageDept
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ExpStorageDept expStorageDept, Model model) {
        List<OrgDept> list = orgDeptService.findList(new OrgDept());
        model.addAttribute("expStorageDept", expStorageDept);
//        model.addAttribute("list", list);
        return "modules/csm/expStorageDeptForm";
    }

    /**
     * 保存并获取科室ID
     *
     * @param expStorageDept
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ExpStorageDept expStorageDept, Model model) {
        StringData data = new StringData();
         /*
        * 验证库房名称的唯一性判断
        * */
        if (expStorageDept.getId() == null || expStorageDept.getId().trim().equals("")) {//添加时时验证
            ExpStorageDept expStorageDept1 = this.get(expStorageDept.getStorageCode());
            if (expStorageDept1 != null) {//通过字节码找到事都有重复数据
                data.setCode("error");
                data.setData("库房名称重复");
                return data;
            }

        }
        int i = expStorageDeptService.saveStorage(expStorageDept);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存消耗品库存单位字典表成功");
        }else if(i == -1){
            data.setCode("error");
            data.setData("该付款单前缀已经存在,请重新维护!");
        }else if(i == -2){
            data.setCode("error");
            data.setData("该入库单号前缀已经存在,请重新维护!");
        }else if(i == -3){
            data.setCode("error");
            data.setData("该出库单号前缀已经存在,请重新维护!");
        } else {
            data.setCode("error");
            data.setData("保存消耗品库存单位字典表失败");
        }
        return data;
    }


    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ExpStorageDept expStorageDept) {
        StringData data = new StringData();
        int i = expStorageDeptService.delete(expStorageDept);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除消耗品库存单位字典表成功");
        } else {
            data.setCode("error");
            data.setData("删除消耗品库存单位字典表失败");
        }
        return data;
    }

    /**
     * @param expStorageDept,q 传递参数
     * @return List<ExpSupplierCata>    返回类型
     * @throws
     * @Title: autoComplete
     * @Desciption: (消耗品库存单位自动补全)
     * @author Zhuq
     * @date 2017-03-12
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoComplete", ""})
    @ResponseBody
    public List<ExpStorageDept> autoComplete(String q, String limit, ExpStorageDept expStorageDept) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<ExpStorageDept> list = expStorageDeptService.findAutoCompleteList(expStorageDept, q, limit);
        return list;
    }


    @RequiresPermissions("user")
    @RequestMapping(value = {"findByMasterClass", ""})
    @ResponseBody
    public List<ExpStorageDept> findByMasterClass(String q, String limit,String masterClass, ExpStorageDept expStorageDept) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<ExpStorageDept> list = expStorageDeptService.findByMasterClass(expStorageDept, q, limit,masterClass);
        return list;
    }

}